package com.sysu.simulation.widget;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sysu.simulation.BaseFragment;
import com.sysu.simulation.R;
import com.sysu.simulation.bean.Clothes;
import com.sysu.simulation.helper.AccountHelper;
import com.sysu.simulation.helper.ApiHelper;

import cz.msebera.android.httpclient.Header;


public class AddClothesFragment extends BaseFragment implements View.OnClickListener{

    private OnClothesAddedListener mListener;
    private Spinner sMaterial, sThickness, sType;
    private EditText mClothesName;
    private Button submit_button;
    private Clothes mClothes;

    public AddClothesFragment() {

    }

    public static AddClothesFragment newInstance() {
        AddClothesFragment fragment = new AddClothesFragment();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnClothesAddedListener) {
            mListener = (OnClothesAddedListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnClothesAddedListener");
        }
    }

    @Override
    protected void initWidget(View root) {
        sMaterial = (Spinner) root.findViewById(R.id.spinner_material);
        sThickness = (Spinner) root.findViewById(R.id.spinner_thickness);
        sType = (Spinner) root.findViewById(R.id.spinner_type);

        mClothesName = (EditText) root.findViewById(R.id.edit_clothesName);
        submit_button = (Button) root.findViewById(R.id.submit_add_clothes);
        submit_button.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        ArrayAdapter<Clothes.Material> materialAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, Clothes.Material.values());
        ArrayAdapter<Clothes.Thickness> thicknessAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, Clothes.Thickness.values());
        ArrayAdapter<Clothes.Type> typeAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, Clothes.Type.values());

        sMaterial.setAdapter(materialAdapter);
        sThickness.setAdapter(thicknessAdapter);
        sType.setAdapter(typeAdapter);

        mClothes = new Clothes();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_add_clothes;
    }

    @Override
    public void onClick(View view) {
        if(checkInput()) {
            mClothes.setTitle(mClothesName.getText().toString().trim());
            mClothes.setMaterial((Clothes.Material)sMaterial.getSelectedItem());
            mClothes.setThickness((Clothes.Thickness)sThickness.getSelectedItem());
            mClothes.setType((Clothes.Type)sType.getSelectedItem());
            doSubmit();
        }
    }

    private boolean checkInput() {
        String tmpClothesName = mClothesName.getText().toString().trim();
        boolean isValid = true;
        View focusView = null;
        if(TextUtils.isEmpty(tmpClothesName)) {
            isValid = false;
            focusView = mClothesName;
            mClothesName.setError("不能为空");
        }

        if(focusView!=null) focusView.requestFocus();
        return isValid;
    }

    public void doSubmit() {
        String uid = AccountHelper.getUserId();
        String jsonClothes = new Gson().toJson(mClothes);
        ApiHelper.addUserClothes(uid, jsonClothes, new TextHttpResponseHandler() {

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Toast.makeText(mContext,"网络请求错误，请重试", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                if(responseString.equals("-1")) {
                    Toast.makeText(mContext,"提交信息不完整，请重试", Toast.LENGTH_SHORT).show();
                }
                else if(responseString.equals("-2")) {
                    Toast.makeText(mContext,"服装名称已存在，请重试", Toast.LENGTH_SHORT).show();
                }
                else if(responseString.equals("-3")) {
                    Toast.makeText(mContext,"添加服装失败，请重试", Toast.LENGTH_SHORT).show();
                }
                else {
                    mListener.onClothesAdded(mClothes);
                }
            }
        });
    }

    public interface OnClothesAddedListener {
        void onClothesAdded(Clothes clothes);
    }
}
